#pragma once 
#include <ncurses.h>
#include <locale.h>
#include <pthread.h>

class Window{
public:
  static void InitWindow(){
    setlocale(LC_ALL, "");  //man 3 setlocale
    initscr();    //初始化curses
    curs_set(0);  //光标设置 --- 0:不可见 1:可见 2:完全可见
    cbreak();     //无buff模式, 键盘输入的字符直接传给程序
    //nocbreak(); //关闭cbreak模式
    //nl();       //回车时换行, 且能被getch读取
    nonl();      //回车不让getch读到, 只是换一行 
    //echo();     //开启回显
    noecho();    //关闭回显功能
    //scrollok(WINDOW*, bool); //设置是否可以滚动屏幕
  }

  static void EndWindow(){
    endwin();
  }

  void SafeWre(pthread_mutex_t *Mutex){
    pthread_mutex_lock(Mutex);
    wrefresh(win);  //这个函数线程不安全
    pthread_mutex_unlock(Mutex);
  }

  void DrawBox(){
    box(win, 0, 0); //默认边框
  }

  void getSize(int &r, int &c){
    row = r;
    col = c;
  }

  void getStart(int &r, int &c){
    r = start_r;
    c = start_c;
  }

  WINDOW* getPoint(){
    return win;
  }

  Window(int _row, int _col, int _start_r, int _start_c)
  :win(nullptr), row(_row), col(_col), start_r(_start_r), start_c(_start_c){
    win = newwin(row, col, start_r, start_c);
  }

  ~Window(){
    if(win){
      delwin(win);
    }
    win = nullptr;
  }

private:
  WINDOW *win;
  int row;
  int col;
  int start_r;
  int start_c;
};
